import Vue from "vue"
import Vuex from "vuex"

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		userInfo: {},
		accessToken: ''
	},
	mutations: {
		// 存储 用户信息 / token
		setState(state, data) {
			for (let key in data) {
				state[key] = data[key]
			}
		},
		// 用户登录后存储数据
		setToken(state, data){
			const { userInfo, access_token } = data
			if(userInfo) {
				state.userInfo = userInfo
				uni.setStorageSync('userInfo', userInfo)
			}
			if(access_token) {
				state.accessToken = access_token
				uni.setStorageSync('accessToken', access_token)
			}
		},	
		// 退出登录
		logout(state){
			state.userInfo = {},
			state.accessToken = '',
			uni.removeStorageSync('userInfo')
			uni.removeStorageSync('accessToken')
		}
	},
	actions: {

	},
	getters: {
		hasLogin(state){
			// 取反两次把原数据转换为布尔类型，有值就是true
			return !!state.accessToken
		}
	}
})

export default store
